home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / ncurses-5.3.lha / ncurses-5.3 / c++ / cursesw.cc < prev    next >
C/C++ Source or Header  |  2002-10-24  |  10KB  |  457 lines

  1. // * this is for making emacs happy: -*-Mode: C++;-*-
  2.  
  3. /*
  4.   Copyright (C) 1989 Free Software Foundation
  5.   written by Eric Newton (newton@rocky.oswego.edu)
  6.  
  7.   This file is part of the GNU C++ Library.  This library is free
  8.   software; you can redistribute it and/or modify it under the terms of
  9.   the GNU Library General Public License as published by the Free
  10.   Software Foundation; either version 2 of the License, or (at your
  11.   option) any later version.  This library is distributed in the hope
  12.   that it will be useful, but WITHOUT ANY WARRANTY; without even the
  13.   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14.   PURPOSE.  See the GNU Library General Public License for more details.
  15.   You should have received a copy of the GNU Library General Public
  16.   License along with this library; if not, write to the Free Software
  17.   Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.   modified by Ulrich Drepper  (drepper@karlsruhe.gmd.de)
  20.           and Anatoly Ivasyuk (anatoly@nick.csh.rit.edu)
  21.  
  22.   modified by Juergen Pfeifer
  23.   Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en
  24. */
  25.  
  26. #include "internal.h"
  27. #include "cursesw.h"
  28.  
  29. MODULE_ID("$Id: cursesw.cc,v 1.25 2002/07/06 15:47:52 juergen Exp $")
  30.  
  31. #define COLORS_NEED_INITIALIZATION  -1
  32. #define COLORS_NOT_INITIALIZED       0
  33. #define COLORS_MONOCHROME            1
  34. #define COLORS_ARE_REALLY_THERE      2
  35.  
  36. // declare static variables for the class
  37. long NCursesWindow::count = 0L;
  38. bool NCursesWindow::b_initialized = FALSE;
  39.  
  40. /*
  41.  * The ncurses library has a fallback for vsscanf(), which may work...
  42.  */
  43. #if !(USE_STRSTREAM_VSCAN || USE_STRSTREAM_VSCAN_CAST)
  44. #  undef  USE_STDIO_VSCAN
  45. #  define USE_STDIO_VSCAN 1
  46. #endif
  47.  
  48. #if defined(__GNUG__)
  49. #  ifndef _IO_va_list
  50. #    define _IO_va_list char *
  51. #  endif
  52. #endif
  53.  
  54. int
  55. NCursesWindow::scanw(const char* fmt, ...)
  56. {
  57.     int result = ERR;
  58.     char buf[BUFSIZ];
  59.  
  60.     if (::wgetnstr(w, buf, sizeof(buf)) != ERR) {
  61.     va_list args;
  62.     va_start(args, fmt);
  63. #if USE_STDIO_VSCAN
  64.     if (::vsscanf(buf, fmt, args) != -1)
  65.         result = OK;
  66. #elif USE_STRSTREAM_VSCAN    /* powerpc, os390 */
  67.     strstreambuf ss(buf, sizeof(buf));
  68.     if (ss.vscan(fmt, args) != -1)
  69.         result = OK;
  70. #elif USE_STRSTREAM_VSCAN_CAST    /* pre-gcc 3.0 */
  71.     strstreambuf ss(buf, sizeof(buf));
  72.     if (ss.vscan(fmt, (_IO_va_list)args) != -1)
  73.         result = OK;
  74. #endif
  75.     va_end(args);
  76.     }
  77.     return result;
  78. }
  79.  
  80.  
  81. int
  82. NCursesWindow::scanw(int y, int x, const char* fmt, ...)
  83. {
  84.     int result = ERR;
  85.     char buf[BUFSIZ];
  86.  
  87.     if (::wmove(w, y, x) != ERR) {
  88.     if (::wgetnstr(w, buf, sizeof(buf)) != ERR) {
  89.         va_list args;
  90.         va_start(args, fmt);
  91. #if USE_STDIO_VSCAN
  92.         if (::vsscanf(buf, fmt, args) != -1)
  93.         result = OK;
  94. #elif USE_STRSTREAM_VSCAN    /* powerpc, os390 */
  95.         strstreambuf ss(buf, sizeof(buf));
  96.         if (ss.vscan(fmt, args) != -1)
  97.         result = OK;
  98. #elif USE_STRSTREAM_VSCAN_CAST    /* pre-gcc 3.0 */
  99.         strstreambuf ss(buf, sizeof(buf));
  100.         if (ss.vscan(fmt, (_IO_va_list)args) != -1)
  101.         result = OK;
  102. #endif
  103.         va_end(args);
  104.     }
  105.     }
  106.     return result;
  107. }
  108.  
  109.  
  110. int
  111. NCursesWindow::printw(const char * fmt, ...)
  112. {
  113.     va_list args;
  114.     va_start(args, fmt);
  115.     char buf[BUFSIZ];
  116.     ::vsprintf(buf, fmt, args);
  117.     va_end(args);
  118.     return waddstr(w, buf);
  119. }
  120.  
  121.  
  122. int
  123. NCursesWindow::printw(int y, int x, const char * fmt, ...)
  124. {
  125.     va_list args;
  126.     va_start(args, fmt);
  127.     int result = ::wmove(w, y, x);
  128.     if (result == OK) {
  129.     char buf[BUFSIZ];
  130.     ::vsprintf(buf, fmt, args);
  131.     result = waddstr(w, buf);
  132.     }
  133.     va_end(args);
  134.     return result;
  135. }
  136.  
  137.  
  138. void
  139. NCursesWindow::init(void)
  140. {
  141.     leaveok(0);
  142.     keypad(1);
  143.     meta(1);
  144. }
  145.  
  146. void
  147. NCursesWindow::err_handler(const char *msg) const THROWS(NCursesException)
  148. {
  149.   THROW(new NCursesException(msg));
  150. }
  151.  
  152. void
  153. NCursesWindow::initialize() {
  154.   if (!b_initialized) {
  155.     ::initscr();
  156.     b_initialized = TRUE;
  157.     if (colorInitialized==COLORS_NEED_INITIALIZATION) {
  158.       colorInitialized=COLORS_NOT_INITIALIZED;
  159.       useColors();
  160.     }
  161.     ::noecho();
  162.     ::cbreak();
  163.   }
  164. }
  165.  
  166. NCursesWindow::NCursesWindow() {
  167.   initialize();
  168.  
  169.   w = (WINDOW *)0;
  170.   init();
  171.   alloced = FALSE;
  172.   subwins = par = sib = 0;
  173.   count++;
  174. }
  175.  
  176. NCursesWindow::NCursesWindow(int lines, int cols, int begin_y, int begin_x)
  177. {
  178.     initialize();
  179.  
  180.     w = ::newwin(lines, cols, begin_y, begin_x);
  181.     if (w == 0) {
  182.     err_handler("Cannot construct window");
  183.     }
  184.     init();
  185.  
  186.     alloced = TRUE;
  187.     subwins = par = sib = 0;
  188.     count++;
  189. }
  190.  
  191. NCursesWindow::NCursesWindow(WINDOW* &window)
  192. {
  193.     initialize();
  194.  
  195.     w = window;
  196.     init();
  197.     alloced = FALSE;
  198.     subwins = par = sib = 0;
  199.     count++;
  200. }
  201.  
  202. NCursesWindow::NCursesWindow(NCursesWindow& win, int l, int c,
  203.                  int begin_y, int begin_x, char absrel)
  204. {
  205.     initialize();
  206.     if (absrel == 'a') { // absolute origin
  207.     begin_y -= win.begy();
  208.     begin_x -= win.begx();
  209.     }
  210.  
  211.     // Even though we treat subwindows as a tree, the standard curses
  212.     // library needs the `subwin' call to link to the parent in
  213.     // order to correctly perform refreshes, etc.
  214.     // Friendly enough, this also works for pads.
  215.     w = ::derwin(win.w, l, c, begin_y, begin_x);
  216.     if (w == 0) {
  217.     err_handler("Cannot construct subwindow");
  218.     }
  219.  
  220.     par = &win;
  221.     sib = win.subwins;
  222.     win.subwins = this;
  223.     subwins = 0;
  224.     alloced = TRUE;
  225.     count++;
  226. }
  227.  
  228. NCursesWindow::NCursesWindow(NCursesWindow& win,
  229.                 bool do_box NCURSES_PARAM_INIT(TRUE))
  230. {
  231.   initialize();
  232.   w = :: derwin(win.w,win.height()-2,win.width()-2,1,1);
  233.   if (w == 0) {
  234.     err_handler("Cannot construct subwindow");
  235.   }
  236.  
  237.   par = &win;
  238.   sib = win.subwins;
  239.   win.subwins = this;
  240.   subwins = 0;
  241.   alloced = TRUE;
  242.   count++;
  243.  
  244.   if (do_box) {
  245.     win.box();
  246.     win.touchwin();
  247.   }
  248. }
  249.  
  250. NCursesWindow NCursesWindow::Clone() {
  251.   WINDOW *d = ::dupwin(w);
  252.   NCursesWindow W(d);
  253.   W.subwins = subwins;
  254.   W.sib = sib;
  255.   W.par = par;
  256.   W.alloced = alloced;
  257.   return W;
  258. }
  259.  
  260. typedef int (*RIPOFFINIT)(NCursesWindow&);
  261. static RIPOFFINIT R_INIT[5];       // There can't be more
  262. static int r_init_idx   = 0;
  263. static RIPOFFINIT* prip = R_INIT;
  264.  
  265. extern "C" int _nc_ripoffline(int,int (*init)(WINDOW*,int));
  266.  
  267. NCursesWindow::NCursesWindow(WINDOW *win, int cols) {
  268.   initialize();
  269.   w = win;
  270.   assert((w->_maxx+1)==cols);
  271.   alloced = FALSE;
  272.   subwins = par = sib = 0;
  273. }
  274.  
  275. int NCursesWindow::ripoff_init(WINDOW *w, int cols)
  276. {
  277.   int res = ERR;
  278.  
  279.   RIPOFFINIT init = *prip++;
  280.   if (init) {
  281.     NCursesWindow* W = new NCursesWindow(w,cols);
  282.     res = init(*W);
  283.   }
  284.   return res;
  285. }
  286.  
  287. int NCursesWindow::ripoffline(int ripoff_lines,
  288.                   int (*init)(NCursesWindow& win)) {
  289.   int code = ::_nc_ripoffline(ripoff_lines,ripoff_init);
  290.   if (code==OK && init && ripoff_lines) {
  291.     R_INIT[r_init_idx++] = init;
  292.   }
  293.   return code;
  294. }
  295.  
  296. bool
  297. NCursesWindow::isDescendant(NCursesWindow& win) {
  298.   for (NCursesWindow* p = subwins; p != NULL; p = p->sib) {
  299.     if (p==&win)
  300.       return TRUE;
  301.     else {
  302.       if (p->isDescendant(win))
  303.     return TRUE;
  304.     }
  305.   }
  306.   return FALSE;
  307. }
  308.  
  309. void
  310. NCursesWindow::kill_subwindows()
  311. {
  312.     for (NCursesWindow* p = subwins; p != 0; p = p->sib) {
  313.     p->kill_subwindows();
  314.     if (p->alloced) {
  315.         if (p->w != 0)
  316.         ::delwin(p->w);
  317.         p->alloced = FALSE;
  318.     }
  319.     p->w = 0; // cause a run-time error if anyone attempts to use...
  320.     }
  321. }
  322.  
  323.  
  324. NCursesWindow::~NCursesWindow()
  325. {
  326.     kill_subwindows();
  327.  
  328.     if (par != 0) {  // Snip us from the parent's list of subwindows.
  329.     NCursesWindow * win = par->subwins;
  330.     NCursesWindow * trail = 0;
  331.     for (;;) {
  332.         if (win == 0)
  333.         break;
  334.         else if (win == this) {
  335.         if (trail != 0)
  336.             trail->sib = win->sib;
  337.         else
  338.             par->subwins = win->sib;
  339.         break;
  340.         } else {
  341.         trail = win;
  342.         win = win->sib;
  343.         }
  344.     }
  345.     }
  346.  
  347.     if (alloced && w != 0)
  348.     ::delwin(w);
  349.  
  350.     if (alloced) {
  351.       --count;
  352.       if (count == 0) {
  353.     ::endwin();
  354.       }
  355.       else if (count < 0) { // cannot happen!
  356.     err_handler("Too many windows destroyed");
  357.       }
  358.     }
  359. }
  360.  
  361. // ---------------------------------------------------------------------
  362. // Color stuff
  363. //
  364. int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
  365.  
  366. void
  367. NCursesWindow::useColors(void)
  368. {
  369.     if (colorInitialized == COLORS_NOT_INITIALIZED) {
  370.       if (b_initialized) {
  371.     if (::has_colors()) {
  372.       ::start_color();
  373.       colorInitialized = COLORS_ARE_REALLY_THERE;
  374.     }
  375.     else
  376.       colorInitialized = COLORS_MONOCHROME;
  377.       }
  378.       else
  379.     colorInitialized = COLORS_NEED_INITIALIZATION;
  380.     }
  381. }
  382.  
  383. short
  384. NCursesWindow::getcolor(int getback) const
  385. {
  386.     short fore, back;
  387.  
  388.     if (colorInitialized==COLORS_ARE_REALLY_THERE) {
  389.       if (::pair_content((short)PAIR_NUMBER(w->_attrs), &fore, &back))
  390.     err_handler("Can't get color pair");
  391.     }
  392.     else {
  393.       // Monochrome means white on black
  394.       back = COLOR_BLACK;
  395.       fore = COLOR_WHITE;
  396.     }
  397.     return getback ? back : fore;
  398. }
  399.  
  400. int NCursesWindow::NumberOfColors()
  401. {
  402.   if (colorInitialized==COLORS_ARE_REALLY_THERE)
  403.     return COLORS;
  404.   else
  405.     return 1; // monochrome (actually there are two ;-)
  406. }
  407.  
  408. short
  409. NCursesWindow::getcolor() const
  410. {
  411.   if (colorInitialized==COLORS_ARE_REALLY_THERE)
  412.     return PAIR_NUMBER(w->_attrs);
  413.   else
  414.     return 0; // we only have pair zero
  415. }
  416.  
  417. int
  418. NCursesWindow::setpalette(short fore, short back, short pair)
  419. {
  420.   if (colorInitialized==COLORS_ARE_REALLY_THERE)
  421.     return ::init_pair(pair, fore, back);
  422.   else
  423.     return OK;
  424. }
  425.  
  426. int
  427. NCursesWindow::setpalette(short fore, short back)
  428. {
  429.   if (colorInitialized==COLORS_ARE_REALLY_THERE)
  430.     return setpalette(fore, back, (short)PAIR_NUMBER(w->_attrs));
  431.   else
  432.     return OK;
  433. }
  434.  
  435.  
  436. int
  437. NCursesWindow::setcolor(short pair)
  438. {
  439.   if (colorInitialized==COLORS_ARE_REALLY_THERE) {
  440.     if ((pair < 1) || (pair > COLOR_PAIRS))
  441.       err_handler("Can't set color pair");
  442.  
  443.     attroff(A_COLOR);
  444.     attrset(COLOR_PAIR(pair));
  445.   }
  446.   return OK;
  447. }
  448.  
  449. #if HAVE_HAS_KEY
  450. extern "C" int _nc_has_mouse(void);
  451.  
  452. bool NCursesWindow::has_mouse() const {
  453.   return ((::has_key(KEY_MOUSE) || ::_nc_has_mouse())
  454.       ? TRUE : FALSE);
  455. }
  456. #endif
  457.